var ahintvl;
function oneclkh(run){
if(run==1){
ahintvl = setInterval(function(){
var button = $(".popup-bg > .footer-buttons button:first-child");
if (button.text() != "" && button.text() != 'Приговорить' && button.text() != 'Рассказать') {
button.click();
}
},50);
}
else if(run==0){
clearInterval(ahintvl);
}
}

function autoshout(run){
if(run==1){
var intvlp = $('#shoutfreq').val();
var intvl = intvlp+'000';
var ist = $('#shoutcol').val();
var fraze = $('#shoutfrz').val();
var autosh = setInterval(function(){
if(ist==1){
clearInterval(autosh);
$('#asht').removeAttr('checked');
}
_CHT_action('', 'smile', ''+fraze, event);
_DLG('scream', 0, event);
_DLG('scream', 2, event);
ist--;
},intvl);
}
else if(run==0)
{

}
}

var avhodin;
function avhin(run){
    if(run==1){
var nname = $('#sname').val();
avhodin = setInterval(function(){
var create = $('#gml_list').find('.row-1').find('span')
for(var i=0;i<create.length;i++){
if($($(create)[i]).text()==nname){
$($(gml_list).find('.row-5')[i]).find('button').click();
break;
}
}
},20);
    }
    else if(run==0){clearInterval(avhodin);}
}

//Выгружаем статистику по нажатии на кнопку
function getcl() {
var i=0;
var result;
var clan = $('#setclan').val();
var s = $('#'+clan).find('tr');
for(var i=0;i<s.length;i++){
var namerow = $($(s[i]).find('td')[1]).text();
var exprow = $($(s[i]).find('td')[3]).text();
result += namerow+" : "+exprow+"\r\n";
}
$('#qeresult').val(result);
};

function getmn(){
var i=0;
var result;
var clan = $('#setclan').val();
var s = $('#'+clan).find('tr');
for(var i=0;i<s.length;i++){
var namerow = $($(s[i]).find('td')[1]).text();
var goldrow = $($(s[i]).find('td')[2]).text();
result += namerow+" : "+goldrow+"\r\n";
}
$('#qeresult').val(result);
}

//Чистим текстареа со статистикой
function clres() {
$('#qeresult').val('');
};

//Открытие профилей
function opprf() {
var i=0;
var clan = $('#setclan').val();
var s = $('#'+clan).find('tr');
for(var i=0;i<s.length;i++){
var click = $($(s[i]).find('td')[1]).find('a').click();
}
};

var newbtn = '<div class="inline" style="padding-right:4px; z-index: 10;"><img src="http://www.iconsearch.ru/uploads/icons/softwaredemo/16x16/cancel__red.png" width="16" height="16" style="vertical-align: middle; padding-right:5px" /><a style="text-decoration:underline;" id="cheatmenu" hidefocus="true" onfocus="this.blur();" ondblclick="return false">Toolset</a></div>';
newbtn += '<img src="http://www.iconsearch.ru/uploads/icons/gnomeicontheme/16x16/stock_certificate.png" width="16" height="16" style="vertical-align: middle; padding-right:5px" /><a style="text-decoration:underline;" id="officermenu" hidefocus="true" onfocus="this.blur();" ondblclick="return false">Клан</a></div>';
var btndiv = '<div id="mdiv" style="background: url(/images/interface/right-column-top.png) repeat-x top;color: #ddd; font-size: 11px; font-family: Tahoma; float: right; margin-top: -36px; height: 16px;"></div>';
$('html').append(btndiv);
$('#mdiv').prepend(newbtn);

var popupcheat = '<div id="wnd_cheat" class="popup-move clans-popup ui-draggable" style="top: 100%; margin-top:-281px; right: 5px; display: block; z-index: 10; height:300px; width:300px;">';
popupcheat += '<div class="popup-bg" id="chset">';
popupcheat += '<table>';
popupcheat += '<tr><td width="40px;">Ход:</td><td><input type="checkbox" id="oneclk" value="1" onclick="update_settings()" /></td></tr>';
popupcheat += '<tr><td width="40px;">Автокрик:</td><td><input type="checkbox" id="asht" value="1" onclick="update_settings()" /><input type="text" id="shoutfreq" placeholder="Интервал" size="4"><input type="text" id="shoutcol" placeholder="Количество" size="4"><br /><span style="margin-left: 20px;"><input type="text" id="shoutfrz" placeholder="Фраза" size="13"></span></td></tr>';
popupcheat += '<tr><td width="40px;">Автовход:</td><td><input type="checkbox" id="avsozd" value="1" onclick="update_settings()" /><input type="text" id="sname" placeholder="Ник" size="9"></td></tr>';
popupcheat += '</table>';
popupcheat += '</div>';
popupcheat += '<a id="closecheat" onclick="$(\'#wnd_cheat\').remove();" class="popup-close" hidefocus="true"></a>';
popupcheat += '<div></div>';

var popupofficer = '<div id="wnd_officer" class="popup-move clans-popup ui-draggable" style="top: 100%; margin-top:-281px; right: 5px; display: block; z-index: 10; height:330px; width:240px;">';
popupofficer += '<div class="popup-bg" id="ofset">';
popupofficer += '<p><select id="setclan"><option value="clu_6213_div">ПНХ</option><option value="clu_6301_div">Палата</option></select></p>';
popupofficer += '<p><span>Статистика клана</span></p><p><textarea cols="25" rows="5" id="qeresult"></textarea></p><p><button id="stats" class="button" onclick="getcl()">Выгрузить опыт</button><button id="stats" class="button" onclick="getmn()">Выгрузить монеты</button>';
popupofficer += '<button id="clearst" class="button" onclick="clres()">Очистить</button></p><p><span>Профили</span></p><p><button id="clanprf" class="button" onclick="opprf()">Открыть все профили</button></p>';
popupofficer += '</div>';
popupofficer += '<a id="closecheat" onclick="$(\'#wnd_officer\').remove();" class="popup-close" hidefocus="true"></a>';
popupofficer += '<div></div>';

$('#cheatmenu').click(function(){
$('#popup_container').append(popupcheat);
$('#wnd_cheat').draggable();
check_settings();
});

$('#officermenu').click(function(){
$('#popup_container').append(popupofficer);    
$('#wnd_officer').draggable();
});

var ashout = 0;
var oclk = 0;
var anhz = 0;

function update_settings(){
if($("#oneclk").prop("checked")){
oclk = 1;
oneclkh(1);
}else{ oclk = 0; oneclkh(0); }

if($("#asht").prop("checked")){
ashout = 1;
autoshout(1);
}else{ ashout = 0; autoshout(0); }

if($("#avsozd").prop("checked")){
anhz = 1;
avhin(1);
}else{ anhz = 0; avhin(0); }
}


function check_settings(){
if(oclk == 1){
$('#oneclk').attr('checked','');
}else{
$('#oneclk').removeAttr('checked');
}

if(ashout == 1){
$('#asht').attr('checked','');
}else{
$('#asht').removeAttr('checked');
}

if(anhz == 1){
$('#avsozd').attr('checked','');
}else{
$('#avsozd').removeAttr('checked');
}
};

$($('body').children('div')[$('body').children('div').length-1]).text('');